<template>
  <div class="login-container">
    <!-- 登录框 -->
    <div class="login-box">
      <h2 class="login-title">登录账号</h2>
      <!-- 注册引导 -->
      <p class="register-tip">
        还没有账号？
        <a href="#" @click="toNote">立即注册</a>
      </p>

      <!-- 表单 -->
      <form class="login-form">
        <div class="form-group">
          <label class="form-label" for="username">用户名</label>
          <input
            type="text"
            id="username"
            placeholder="请输入用户名"
            class="form-input"
            v-model="form.username"
          />
        </div>

        <div class="form-group">
          <label class="form-label" for="password">密码</label>
          <input
            type="password"
            id="password"
            placeholder="请输入密码"
            class="form-input"
            v-model="form.password"
          />
        </div>
        <div class="form-group captcha-group">
          <label class="form-label" for="vercode">验证码</label>
          <div class="captcha-row">
            <!-- 验证码输入框（保留） -->
            <input
              type="text"
              id="vercode"
              placeholder="请输入验证码"
              class="form-input captcha-input"
              v-model="form.vercode"
            />
            <!-- 新增：获取验证码按钮 + 加载态 + 文字显示 -->
            <button
              type="button"
              class="captcha-btn"
              :class="{ loading: isCaptchaLoading }"
              @click="fetchCaptcha"
            >
              {{ isCaptchaLoading ? "获取中..." : "点击获取验证码" }}
            </button>
            <!-- 验证码文字显示（接口返回后展示） -->
            <span class="captcha-text" v-if="captchaText">
              验证码：{{ captchaText }}
            </span>
          </div>
        </div>
        <div class="form-group remember-group">
          <input type="checkbox" id="remember" v-model="form.rememberMe" />
          <label for="remember" class="remember-label">记住我</label>
        </div>

        <!-- 登录按钮 -->
        <button type="button" class="login-btn" @click="handleLogin">
          登录
        </button>
      </form>
    </div>
  </div>
</template>
<script>
//接受接口
import htpps from "@/urtil/https";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
        rememberMe: false,
        vercode: "",
      },
      captchaText: "", // 新增，与模板绑定
      isCaptchaLoading: false, // 加载状态
    };
  },
  created() {
    this.fetchCaptcha(); // 初始化自动加载验证码
  },
  methods: {
    toNote: function () {
      this.$router.push("/Register");
    },
    handleLogin: function () {
      // var a = JSON.parse(localStorage.getItem("userCredentials"));
      // 验证用户名和密码
      htpps
        .LoginAllData({
          name: this.form.username,
          pwd: this.form.password,
          vercode: this.form.vercode,
        })
        .then((val) => {
          const loginArr = localStorage.getItem("LoginArr");
          let isAuthPass = false; // 校验标记
          if (val.code === 1) {
            alert(val.msg + "请查看账号和密码是否正确");
          } else if (val.code === 3) {
            alert(val.msg);
          }
          // 空值校验
          else if (this.form.password === "") {
            alert("密码不能为空!");
          } else if (this.form.username === "") {
            alert("用户名不能为空");
          } else if (this.form.vercode === "") {
            alert("验证码不能为空");
          } else if (loginArr) {
            const a = JSON.parse(loginArr);
            if (this.form.username !== a.name) {
              alert("账号不一致!");
            } else if (this.form.password !== a.password) {
              alert("密码不一致!");
            } else {
              isAuthPass = true; // 所有校验通过
            }
          }
          // 校验通过后，跳转首页
          if (isAuthPass) {
            this.$router.push("/Homepage");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 新增：刷新验证码（调用新接口 /getcode）
    fetchCaptcha() {
      htpps
        .LoginVercode()
        .then((val) => {
          this.captchaText = val.msg;
          console.log(this.captchaText);
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style scoped>
/* 容器：让登录框居中 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa; /* 页面背景色 */
}

/* 登录框样式 */
.login-box {
  width: 360px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 24px 32px;
}

/* 标题 */
.login-title {
  margin: 0 0 16px;
  font-size: 20px;
  font-weight: 600;
  color: #333;
}

/* 注册引导 */
.register-tip {
  margin: 0 0 24px;
  font-size: 14px;
  color: #666;
}
.register-link {
  color: #409eff; /* 突出注册链接 */
  text-decoration: none;
  margin-left: 4px;
}

/* 表单组通用样式 */
.form-group {
  margin-bottom: 16px;
}
.form-label {
  display: block;
  margin-bottom: 6px;
  font-size: 14px;
  color: #333;
}
.form-input {
  width: 100%;
  padding: 10px;
  border: 1px solid #dcdcdc;
  border-radius: 4px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}
.form-input:focus {
  border-color: #409eff;
}

.remember-label {
  font-size: 14px;
  color: #666;
  cursor: pointer;
}
.forgot-link {
  font-size: 14px;
  color: #409eff;
  text-decoration: none;
}

/* 第三方登录区域 */
.third-party-login {
  margin: 24px 0;
  text-align: center;
}
.third-party-text {
  font-size: 14px;
  color: #999;
  margin-bottom: 12px;
}
.third-party-buttons {
  display: flex;
  justify-content: center;
  gap: 8px;
}
.third-btn {
  padding: 8px 16px;
  border: 1px solid #dcdcdc;
  border-radius: 4px;
  background-color: #fff;
  cursor: pointer;
  transition: all 0.2s;
}
.third-btn:hover {
  border-color: #409eff;
  color: #409eff;
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.2s;
}
.login-btn:hover {
  background-color: #66b1ff;
}
/* 验证码容器：调整间距 */
.captcha-group {
  margin-top: 16px;
}

/* 行布局：对齐 + 间距 */
.captcha-row {
  display: flex;
  align-items: center;
  gap: 10px; /* 元素间距 */
  flex-wrap: wrap; /* 小屏幕自动换行 */
}

/* 输入框：继承原有样式，补充焦点效果 */
.captcha-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #dcdcdc;
  border-radius: 4px;
  transition: border-color 0.3s;
}
.captcha-input:focus {
  border-color: #409eff;
  box-shadow: 0 0 6px rgba(64, 158, 255, 0.2);
}

/* 验证码按钮：主色调 + 交互反馈 */
.captcha-btn {
  padding: 8px 16px;
  background: linear-gradient(45deg, #409eff, #66b1ff);
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.3s, box-shadow 0.3s;
}
.captcha-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}
.captcha-btn:active {
  transform: translateY(0);
  box-shadow: none;
}

/* 加载态：按钮样式 + 动画 */
.captcha-btn.loading {
  background: #c6e2ff;
  cursor: not-allowed;
  position: relative;
}
/* 加载动画（Spinner） */
.captcha-btn.loading::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 12px;
  width: 14px;
  height: 14px;
  border: 2px solid #fff;
  border-radius: 50%;
  border-top-color: transparent;
  animation: spin 0.8s linear infinite;
  transform: translateY(-50%);
}

/* 验证码文字提示：柔和配色 */
.captcha-text {
  color: #666;
  font-size: 14px;
  margin-left: 8px;
  opacity: 0.9;
}

/* 关键帧：Spinner 旋转 */
@keyframes spin {
  to {
    transform: translateY(-50%) rotate(360deg);
  }
}
</style>